package com.opgea.crm.services;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.opgea.crm.common.util.DateUtil;
import com.opgea.crm.common.vo.IssueCommentVo;
import com.opgea.crm.common.vo.IssueDashboardVo;
import com.opgea.crm.domain.entities.Employee;
import com.opgea.crm.domain.entities.Issue;
import com.opgea.crm.domain.entities.IssueComment;
import com.opgea.crm.domain.entities.ProjectDetails;
import com.opgea.crm.domain.entities.SetupsValue;
import com.opgea.crm.domain.entities.security.CrmUser;
import com.opgea.crm.repository.IssueRepo;
import com.opgea.crm.repository.SetupsRepo;
@Service
public class IssueServiceImpl implements IssueService{
	
	@Autowired
	IssueRepo issuerepo;
	@Autowired
	ProjectDetailsService projectDetailsService;
	@Autowired
	SetupsRepo setupsRepo;
	
	@Override
	@Transactional(propagation=Propagation.REQUIRES_NEW)
	public Long create(Issue issue) { 
		return  issuerepo.create(issue); 
    }
	@Transactional(propagation=Propagation.REQUIRED)
	@Override
	public void update(Issue issue) {
		issuerepo.update(issue);
	}
	
	public void remove(Long id) {
	}

	public Issue find(Long id) {
		return issuerepo.find(id);
	}

	public List<IssueDashboardVo> findAll() {
		List<IssueDashboardVo>  list=new ArrayList<IssueDashboardVo>();
		for(Issue data:issuerepo.findAll())
		{
			IssueDashboardVo vo=new IssueDashboardVo();
			vo.setIssueId(data.getId());
			vo.setIssueName(data.getIssueName());
		    vo.setIssueReason(data.getIssueReason());
		    vo.setIssueStatus(data.getStatus().getFieldValue());
		    vo.setIssueAssignedTo(data.getAssignedTo().getFirstName()); 
		    list.add(vo);
			System.out.println("Issue :"+vo);
		}
		return list;
	}
 
 public List<IssueDashboardVo> findAllByProjectId(long projectId) { 
		List<IssueDashboardVo>  list=new ArrayList<IssueDashboardVo>();
		ProjectDetails projectDetails = projectDetailsService.find(projectId);
		if(projectDetails!=null)
		for(Issue data:projectDetails.getIssues()){ 
			IssueDashboardVo vo=new IssueDashboardVo();
			vo.setIssueId(data.getId());
			vo.setIssueProjectName(data.getProject().getProjectName());
			vo.setIssueName(data.getIssueName());
			vo.setIssueReason(data.getIssueReason());
			vo.setIssueModule(data.getModuleId());
			if(data.getStatus()!=null){
		    vo.setIssueStatus(data.getStatus().getFieldValue()); 
		    vo.setIssueStatusId(data.getStatus().getId());
			}
			if(data.getAssignedTo()!=null){ 
		    vo.setIssueAssignedTo(data.getAssignedTo().getFirstName());
		    vo.setIssueAssignedToId(data.getAssignedTo().getId());
			}
			if(data.getCategory()!=null){
			vo.setIssueCategory(data.getCategory().getFieldValue());
		    vo.setIssueCategoryId(data.getCategory().getId());
			}
			if(data.getComplexity()!=null){
		    vo.setIssueComplexity(data.getComplexity().getFieldValue());
		    vo.setIssueComplexityId(data.getComplexity().getId());
			}
			if(data.getPriority()!=null){ 
		    vo.setIssuePriority(data.getPriority().getFieldValue());
		    vo.setIssuePriorityId(data.getPriority().getId());
			}
			if(data.getResolution()!=null){
		    vo.setIssueResolution(data.getResolution().getFieldValue());
		    vo.setIssueResolutionId(data.getResolution().getId());
			}
		    vo.setTargetDate(DateUtil.dateToString(data.getTargetDate())); 
		    vo.setDateResolved(DateUtil.dateToString(data.getResolvedDate()));
		    System.out.println("User Name : "+data.getCreatedBy().getUserName());
		    vo.setCreatedBy(data.getCreatedBy().getEmployee().getFirstName());     
		    vo.setDate(DateUtil.dateToString(data.getCreatedAt()));
		    if(data.getProject()!= null)
		    	vo.setIssueProjectId(data.getProject().getId());
		    list.add(vo);
		}
		return list;
	} 
	
  
  public List<IssueDashboardVo> issueByProjectId(Long projectId,Long priorityId,Long assigntoId,CrmUser user) 
	{ 
		List<IssueDashboardVo> list=new ArrayList<IssueDashboardVo>();
		List<Issue> issues = issuerepo.issueByProjectId(projectId, priorityId, assigntoId, user) ; 
		for(Issue issue:issues)  
		  { 
			  IssueDashboardVo issueDashboardVo = new IssueDashboardVo();
			  issueDashboardVo.setIssueId(issue.getId());
			  issueDashboardVo.setIssueProjectName(issue.getProject().getProjectName()); 
			  issueDashboardVo.setIssueName(issue.getIssueName());
			  issueDashboardVo.setIssueReason(issue.getIssueReason()); 
			  if(issue.getCreatedBy()!=null){
					issueDashboardVo.setCreatedBy(issue.getCreatedBy().getUserName()); 
					}
			  if(issue.getAssignedTo()!=null){
			  issueDashboardVo.setIssueAssignedTo(issue.getAssignedTo().getFirstName());
			  issueDashboardVo.setIssueAssignedToId(issue.getAssignedTo().getId());
			  }
			  if(issue.getPriority()!=null){
			  issueDashboardVo.setIssuePriority(issue.getPriority().getFieldValue()); 
			  issueDashboardVo.setIssuePriorityId(issue.getPriority().getId());
			  }
			  if(issue.getStatus()!=null){
			  issueDashboardVo.setIssueStatus(issue.getStatus().getFieldValue());
			  issueDashboardVo.setIssueStatusId(issue.getStatus().getId());
			  }
			  if(issue.getResolution()!=null){
			  issueDashboardVo.setIssueResolution(issue.getResolution().getFieldValue());
			  issueDashboardVo.setIssueResolutionId(issue.getResolution().getId());
			  }
			  issueDashboardVo.setDate(DateUtil.dateToString(issue.getCreatedAt())); 
			  issueDashboardVo.setIssueReason(issue.getIssueReason());
			  issueDashboardVo.setIssueModule(issue.getModuleId()); 
			  if(issue.getCategory()!=null){
			  issueDashboardVo.setIssueCategory(issue.getCategory().getFieldValue());
			  issueDashboardVo.setIssueCategoryId(issue.getCategory().getId());
			  } 
			  if(issue.getComplexity()!= null){
			  issueDashboardVo.setIssueComplexity(issue.getComplexity().getFieldValue());
			  issueDashboardVo.setIssueComplexityId(issue.getComplexity().getId());
			  }
			  issueDashboardVo.setTargetDate(DateUtil.dateToString(issue.getTargetDate()));
			  issueDashboardVo.setDateResolved(DateUtil.dateToString(issue.getResolvedDate()));
			  if(issue.getProject()!= null)
			    	issueDashboardVo.setIssueProjectId(issue.getProject().getId()); 
			 
			  list.add(issueDashboardVo);   
		  }
	    return  list;
	}
	
	@Override
	public List<IssueCommentVo> findAllCommentsByIssueId(long issueId) {
		List<IssueComment> commentList = issuerepo.findAllCommentsByIssueId(issueId);
		List<IssueCommentVo> commentVoList = new ArrayList<IssueCommentVo>();
		for(IssueComment comment : commentList){
			IssueCommentVo commentVo = new IssueCommentVo();
			commentVo.setIssueId(comment.getId()); 
			 commentVo.setUpdatedAt(DateUtil.dateToString(comment.getCreatedAt())+"/"+DateUtil.dateToTime(comment.getCreatedAt()));
			commentVo.setTime(DateUtil.dateToTime(comment.getCreatedAt()));
			String commentString = new String(comment.getComment());
			commentVo.setComment(commentString); 
			if(comment.getStatus()!= null)
			commentVo.setIssueStatus(comment.getStatus().getFieldValue()); 
			if(comment.getAssignedTo()!=null)
			commentVo.setAssignedTo(comment.getAssignedTo().getFirstName());   
			commentVoList.add(commentVo);
		}
		return commentVoList; 
	}
	
	@Override
	public List<SetupsValue> allProjectAndMembers(Long projectId)
	{
		List<ProjectDetails> projectDetails=this.issuerepo.allProjectAndMembers(projectId);
		List<SetupsValue> setupsValues=new ArrayList<SetupsValue>();
		for(ProjectDetails data:projectDetails)
		{
			for(Employee employee:data.getMembers())
			setupsValues.add(new SetupsValue(employee.getId(), (employee.getFirstName()+" "+employee.getLastName())));
		}
		return setupsValues; 
	}
	
	public List<SetupsValue> allProjects()
	{
		return this.issuerepo.allProjects();
	} 
	public Issue findIssueById(Long issueId){
		
			return this.issuerepo.findIssueById(issueId); 
	}
}
